#ifndef __UDSF_RC6011_C_AIP_C__
#define __UDSF_RC6011_C_AIP_C__

#include "app.h"
/*
*   notice:
*   UDSF 同UDF编程：不可调用cstd-lib,只可调用"sdk_ifs_udk_cfg.h"、"sys_api.h"中列出的API函数。
*   UDSF文件内函数均需使用static修饰符，且UDF中以#include "udsf_xxx.c"形式引用
*/

#if (defined(USE_EXRF_RC6011) || defined(USE_EXRF_RC6011C) || defined(USE_EXRF_RC6011AIP) ) && defined(USE_EX_RF)
#include "udsf_ex_rf.c"

#define EX_RF_USE_DRV_QUICK

#define RC6011_EC_OK                   (0)   // 正常
#define RC6011_EC_INIT_ERR             (-1)  // 初始化错误
#define RC6011_EC_ANA_CFG_ERR          (-2)  // 配置ANA错误
#define RC6011_EC_WAVE0_CFG_ERR        (-30) // 配置WAVE0错误
#define RC6011_EC_WAVE1_CFG_ERR        (-31) // 配置WAVE1错误
#define RC6011_EC_WAVE2_CFG_ERR        (-32) // 配置WAVE2错误
#define RC6011_EC_RAMP_CFG_ERR         (-4)  // 配置RAMP错误
#define RC6011_EC_ON_PLL_CFG_ERR       (-5)  // 开启PLL错误
#define RC6011_EC_ON_PLLRFIF_CFG_ERR   (-6)  // 开启PLLRF/IF错误
#define RC6011_EC_OFF_PLLRFIF_CFG_ERR  (-7)  // 关闭PLL/RF/IF错误

static int udsf_rc6011_wave_start(void){
	udsf_ex_rf_command(RC6011_CMD_RAMP_START);
	return RC6011_EC_OK;
}

static int udsf_rc6011_wave_stop(void){
	udsf_ex_rf_command(RC6011_CMD_RAMP_STOP);
	return RC6011_EC_OK;
}

static int udsf_rc6011_wave_clear(void){
	udsf_ex_rf_command(RC6011_CMD_WAV_MEM_CLR);
	return RC6011_EC_OK;
}

static int udsf_rc6011_tx_ctrl(uint8_t tx_en){
	uint8_t cmd = RC6011_CMD_TX_EN_BASE|tx_en;
	udsf_ex_rf_command(cmd);
	return RC6011_EC_OK;
}

static int udsf_rc6011_ramp_wave_cfg(uint8_t row_addr,struct ramp_wave_unit *p_wave){
	struct ramp_wave_unit ramp_wave_cfg;
	
	#if !defined(EX_RF_USE_DRV_QUICK)
	udsf_ex_rf_wavemem_write(row_addr,WAVEMEM_COL_ADDR_7,p_wave->a_bandwidth,&ramp_wave_cfg.a_bandwidth);
	udsf_ex_rf_wavemem_write(row_addr,WAVEMEM_COL_ADDR_6,p_wave->a_time,&ramp_wave_cfg.a_time);
	udsf_ex_rf_wavemem_write(row_addr,WAVEMEM_COL_ADDR_5,p_wave->b_bandwidth,&ramp_wave_cfg.b_bandwidth);
	udsf_ex_rf_wavemem_write(row_addr,WAVEMEM_COL_ADDR_4,p_wave->b_time,&ramp_wave_cfg.b_time);
	udsf_ex_rf_wavemem_write(row_addr,WAVEMEM_COL_ADDR_3,p_wave->c_bandwidth,&ramp_wave_cfg.c_bandwidth);
	udsf_ex_rf_wavemem_write(row_addr,WAVEMEM_COL_ADDR_2,p_wave->c_time,&ramp_wave_cfg.c_time);
	udsf_ex_rf_wavemem_write(row_addr,WAVEMEM_COL_ADDR_1,p_wave->r,&ramp_wave_cfg.r);
	udsf_ex_rf_wavemem_write(row_addr,WAVEMEM_COL_ADDR_0,p_wave->v,&ramp_wave_cfg.v);
	#else
	uint8_t col_addr[]={WAVEMEM_COL_ADDR_7,WAVEMEM_COL_ADDR_6,WAVEMEM_COL_ADDR_5,WAVEMEM_COL_ADDR_4,WAVEMEM_COL_ADDR_3,WAVEMEM_COL_ADDR_2,WAVEMEM_COL_ADDR_1,WAVEMEM_COL_ADDR_0};
	udsf_ex_rf_wavemem_wr_quick(row_addr,col_addr,(uint16_t*)p_wave,(uint16_t*)&ramp_wave_cfg,sizeof(col_addr));
	#endif
	
	// udsf_uart_send_u16_base16(1,(uint16_t*)p_wave,8,"wave wr:","\n");
	// udsf_uart_send_u16_base16(1,(uint16_t*)&ramp_wave_cfg,8,"wave rd:","\n");
	
	return udsf_memcmp((uint8_t*)&ramp_wave_cfg,(uint8_t*)p_wave,sizeof(struct ramp_wave_unit));
}

static int udsf_rc6011_ramp_cfg(struct ramp_cfg *p_ramp){
	struct ramp_cfg ramp_cfg;

	#if !defined(EX_RF_USE_DRV_QUICK)
	udsf_ex_rf_reg_write(RC6011_RAMP_PLL_CFG0_ADDR,   p_ramp->pll_cfg0, &ramp_cfg.pll_cfg0);
	udsf_ex_rf_reg_write(RC6011_RAMP_PLL_CFG1_ADDR,   p_ramp->pll_cfg1, &ramp_cfg.pll_cfg1);
	udsf_ex_rf_reg_write(RC6011_RAMP_SYNCW_CFG0_ADDR, p_ramp->syncw_cfg0, &ramp_cfg.syncw_cfg0);
	udsf_ex_rf_reg_write(RC6011_RAMP_SYNCW_CFG1_ADDR, p_ramp->syncw_cfg1, &ramp_cfg.syncw_cfg1);
	udsf_ex_rf_reg_write(RC6011_RAMP_WM_CFG0_ADDR,    p_ramp->wm_cfg0, &ramp_cfg.wm_cfg0);
	udsf_ex_rf_reg_write(RC6011_RAMP_WM_CFG1_ADDR,    p_ramp->wm_cfg1, &ramp_cfg.wm_cfg1);
	udsf_ex_rf_reg_write(RC6011_RAMP_ACCW_CFG0_ADDR,  p_ramp->accw_cfg0, &ramp_cfg.accw_cfg0);
	udsf_ex_rf_reg_write(RC6011_RAMP_ACCW_CFG1_ADDR,  p_ramp->accw_cfg1, &ramp_cfg.accw_cfg1);	
	#else
	uint8_t cmd[]={ RC6011_RAMP_PLL_CFG0_ADDR,
		              RC6011_RAMP_PLL_CFG1_ADDR,
	                RC6011_RAMP_SYNCW_CFG0_ADDR,
		              RC6011_RAMP_SYNCW_CFG1_ADDR,
	                RC6011_RAMP_WM_CFG0_ADDR,
	                RC6011_RAMP_WM_CFG1_ADDR,
                  RC6011_RAMP_ACCW_CFG0_ADDR,
                  RC6011_RAMP_ACCW_CFG1_ADDR };
	
	udsf_ex_rf_reg_wr_quick(cmd,(uint16_t*)p_ramp,(uint16_t*)&ramp_cfg,sizeof(cmd));
	#endif	
	//udsf_uart_send_u16_base16(1,(uint16_t*)p_ramp,8,"ramp wr:","\n");
	//udsf_uart_send_u16_base16(1,(uint16_t*)&ramp_cfg,8,"ramp rd:","\n");
	return udsf_memcmp((uint8_t*)&ramp_cfg,(uint8_t*)p_ramp,sizeof(struct ramp_cfg));
}


static int udsf_rc6011_ana_cfg_rf_if_on(void){

	#if UDFDEF_CYCLE_NUM == 1 // 只采集一个大chirp，pll不在此处开启
	uint8_t cmd[] = {RC6011_ANA_CFG8_ADDR,RC6011_ANA_CFG22_ADDR};
	uint16_t data[] = {UDPCFG_EX_ANA_ON_CFG8,UDPCFG_EX_ANA_ON_CFG22};
	uint16_t read_data[2] = {0};
	#else
	uint8_t cmd[] = {RC6011_ANA_CFG18_ADDR,RC6011_ANA_CFG8_ADDR,RC6011_ANA_CFG22_ADDR};
	uint16_t data[] = {UDPCFG_EX_ANA_ON_CFG18,UDPCFG_EX_ANA_ON_CFG8,UDPCFG_EX_ANA_ON_CFG22};
	uint16_t read_data[3] = {0};
	#endif
	
	#if !defined(EX_RF_USE_DRV_QUICK)
	for( int i=0;i<sizeof(read_data)/sizeof(read_data[0]);i++){
		udsf_ex_rf_reg_write(cmd[i],data[i],&read_data[i]);
	}
	#else
	udsf_ex_rf_reg_wr_quick(cmd,data,(uint16_t*)&read_data,sizeof(read_data)/sizeof(read_data[0]));
	#endif
	if( udsf_memcmp((uint8_t*)data,(uint8_t*)read_data,sizeof(read_data)) < 0 ){
		return RC6011_EC_ON_PLLRFIF_CFG_ERR;
	}
	return RC6011_EC_OK;
}

static int udsf_rc6011_ana_cfg_rf_if_off(void){

	uint8_t cmd[] = {RC6011_ANA_CFG22_ADDR,RC6011_ANA_CFG18_ADDR,RC6011_ANA_CFG8_ADDR};
	uint16_t data[] = {UDPCFG_EX_ANA_OFF_CFG22,UDPCFG_EX_ANA_OFF_CFG18,UDPCFG_EX_ANA_OFF_CFG8};
	uint16_t read_data[3] = {0};

	#if !defined(EX_RF_USE_DRV_QUICK)
	for( int i=0;i<sizeof(read_data)/sizeof(read_data[0]);i++){
		udsf_ex_rf_reg_write(cmd[i],data[i],&read_data[i]);
	}
	#else
	udsf_ex_rf_reg_wr_quick(cmd,data,(uint16_t*)&read_data,sizeof(read_data)/sizeof(read_data[0]));
	#endif
	if( udsf_memcmp((uint8_t*)data,(uint8_t*)read_data,sizeof(read_data)) < 0 ){
		return RC6011_EC_OFF_PLLRFIF_CFG_ERR;
	}
	return RC6011_EC_OK;
}

static int udsf_rc6011_ana_cfg(struct ana_unit * p_ana ){

	struct ana_unit ana_cfg;
	
	#define ANA_CFG_ONLY_NECCESSARY

	#if !defined(EX_RF_USE_DRV_QUICK)
	udsf_ex_rf_reg_write(RC6011_ANA_CFG0_ADDR, p_ana->cfg0, &ana_cfg.cfg0);
	udsf_ex_rf_reg_write(RC6011_ANA_CFG1_ADDR, p_ana->cfg1, &ana_cfg.cfg1);
	udsf_ex_rf_reg_write(RC6011_ANA_CFG2_ADDR, p_ana->cfg2, &ana_cfg.cfg2);
	
	udsf_ex_rf_reg_write(RC6011_ANA_CFG9_ADDR, p_ana->cfg9, &ana_cfg.cfg9);
	udsf_ex_rf_reg_write(RC6011_ANA_CFG10_ADDR,p_ana->cfg10,&ana_cfg.cfg10);
	udsf_ex_rf_reg_write(RC6011_ANA_CFG19_ADDR,p_ana->cfg19,&ana_cfg.cfg19);
	
	udsf_ex_rf_reg_write(RC6011_ANA_CFG20_ADDR,p_ana->cfg20,&ana_cfg.cfg20);
	udsf_ex_rf_reg_write(RC6011_ANA_CFG21_ADDR,p_ana->cfg21,&ana_cfg.cfg21);
	#if !defined(ANA_CFG_ONLY_NECCESSARY)
	udsf_ex_rf_reg_write(RC6011_ANA_CFG18_ADDR,p_ana->cfg18,&ana_cfg.cfg18);
	udsf_ex_rf_reg_write(RC6011_ANA_CFG8_ADDR, p_ana->cfg8, &ana_cfg.cfg8);
	udsf_ex_rf_reg_write(RC6011_ANA_CFG22_ADDR,p_ana->cfg22,&ana_cfg.cfg22);
	#endif

	#else
	uint8_t cmd[]={ RC6011_ANA_CFG0_ADDR,
					RC6011_ANA_CFG1_ADDR,
					RC6011_ANA_CFG2_ADDR,
					RC6011_ANA_CFG9_ADDR,
					RC6011_ANA_CFG10_ADDR,
					RC6011_ANA_CFG19_ADDR,
					RC6011_ANA_CFG20_ADDR,
					RC6011_ANA_CFG21_ADDR,
					#if !defined(ANA_CFG_ONLY_NECCESSARY)
					RC6011_ANA_CFG18_ADDR,
					RC6011_ANA_CFG8_ADDR,
					RC6011_ANA_CFG22_ADDR };
					#else
					};
					#endif

	udsf_ex_rf_reg_wr_quick(cmd,(uint16_t*)p_ana,(uint16_t*)&ana_cfg,sizeof(cmd));
	#endif
	
	#if !defined(ANA_CFG_ONLY_NECCESSARY)
	//LOG_SEND_U8_BASE16(LOG_LEVEL_EMERG,(uint8_t*)p_ana,sizeof(struct ana_unit)-2,"ana wr:","\n");
	//LOG_SEND_U8_BASE16(LOG_LEVEL_EMERG,(uint8_t*)&ana_cfg,sizeof(struct ana_unit)-2,"ana rd:","\n");
	return udsf_memcmp((uint8_t*)&ana_cfg,(uint8_t*)p_ana,sizeof(struct ana_unit)-2);
	#else
	//LOG_SEND_U8_BASE16(LOG_LEVEL_EMERG,(uint8_t*)p_ana,sizeof(struct ana_unit)-2-6,"ana wr:","\n");
	//LOG_SEND_U8_BASE16(LOG_LEVEL_EMERG,(uint8_t*)&ana_cfg,sizeof(struct ana_unit)-2-6,"ana rd:","\n");
	return udsf_memcmp((uint8_t*)&ana_cfg,(uint8_t*)p_ana,sizeof(struct ana_unit)-2-6);
	#endif
}

static void udsf_rc6011_deinit(void){
	udsf_ex_rf_spi_deinit();
	udsf_ex_rf_chip_ctrl(0);
}

static int udsf_rc6011_init(void)
{	
	udsf_ex_rf_spi_init(0);// 0~15	
	
	//int res =  
	udsf_ex_rf_chip_ctrl(1);
	udsf_delay_us(100);// 调试实测需要至少60us
	return RC6011_EC_OK;
}



static int udsf_rc6011_ramp_mode_config(void)
{
	int res = RC6011_EC_OK;
	
	do{
		#if UDFDEF_CYCLE_NUM == 1 // 只采集一个大chirp 提前开pll
		uint8_t cmd[] = {RC6011_ANA_CFG18_ADDR};
		uint16_t data[] = {UDPCFG_EX_ANA_ON_CFG18};
		uint16_t read_data[1] = {0};

		#if !defined(EX_RF_USE_DRV_QUICK)
		for( int i=0;i<sizeof(read_data)/sizeof(read_data[0]);i++){
			udsf_ex_rf_reg_write(cmd[i],data[i],&read_data[i]);
		}
		#else
		udsf_ex_rf_reg_wr_quick(cmd,data,(uint16_t*)&read_data,sizeof(read_data)/sizeof(read_data[0]));
		#endif
		if( read_data[0] != data[0] ){
			res = RC6011_EC_ON_PLL_CFG_ERR;
			break;
		}
		#endif

		if( udsf_rc6011_ramp_cfg(&paramANA->ramp_cfg) < 0 ){
			res = RC6011_EC_RAMP_CFG_ERR;
			break;
		}
	}while(0);
	return res;
}

static int udsf_rc6011_ana_config(void)
{
	int res = RC6011_EC_OK;

	do{
		/* ana cfg */
		if( udsf_rc6011_ana_cfg(&paramANA->ana_on) < 0 ){
			res = RC6011_EC_ANA_CFG_ERR;
			break;
		}
	}while(0);
	
	return res;
}

static int udsf_rc6011_ramp_wave_config(void)
{
	int res = RC6011_EC_OK;
	
	do{
		if( udsf_rc6011_ramp_wave_cfg(0,&paramANA->ramp_wave0) < 0 ){
			res = RC6011_EC_WAVE0_CFG_ERR;
			break;
		}

		paramANA->ramp_wave1.v &= ~(0xf<<12);
		paramANA->ramp_wave1.v |= (1<<12); // only TX1 enable
		if( udsf_rc6011_ramp_wave_cfg(1,&paramANA->ramp_wave1) < 0 ){
			res = RC6011_EC_WAVE1_CFG_ERR;
			break;
		}
		#if (UDFDEF_TX_NUM > 1 )
		paramANA->ramp_wave1.v &= ~(0xf<<12);
		paramANA->ramp_wave1.v |= (2<<12); // only TX2 enable
		if( udsf_rc6011_ramp_wave_cfg(2,&paramANA->ramp_wave1) < 0 ){
			res = RC6011_EC_WAVE2_CFG_ERR;
			break;
		}
		#endif
	}while(0);
	
	return res;
}

#endif

#endif
